<template>
  <div>

    <!--定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">

      <!-- 定义栅格 -->
      <el-row>
        <el-col>
          <el-button type="primary">新增角色</el-button>
        </el-col>
      </el-row>

      <!-- 定义表格数据 border设定边框线 -->
      <el-table :data="roleList" style="width: 100%" border stripe>

        <!-- 展现展开按钮-->
        <el-table-column type="expand">
          <!-- 定义作用域插槽-->
          <template slot-scope="scope">

            <!-- 根据栅格系统定义页面结构 -->
            <el-row :class="['borderbottom',index===0 ? 'borderTop' : '','vcenter']"
                    v-for="(rights1,index) in scope.row.rightsList" :key="rights1.id">

              <!-- 通过tab标签定义一级标签 -->
              <el-col :span="5">
                <el-tag closable @close="deleteRights(scope.row,rights1,1)">{{rights1.name}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <!-- 定义所有的二级标签-->
                <el-row v-for="(rights2,index2) in rights1.children" :key="rights2.id"
                        :class="[index2 === 0 ? '' : 'borderTop','vcenter']">
                  <el-col :span="6">
                    <el-tag type="success" closable @close="deleteRights(scope.row,rights2,2)">{{rights2.name}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="13">
                    <el-tag type="warning" v-for="rights3 in rights2.children" :key="rights3.id" closable
                            @close="deleteRights(scope.row,rights3,3)">{{rights3.name}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- 格式化页面数据-->
            <!-- <pre>{{scope.row}}</pre> -->
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDuty" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <!-- 定义作用域插槽-->
          <template slot-scope="scope">
            <!-- 定义表格按钮-->
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
            <el-button type="warning" size="mini" icon="el-icon-setting" @click="showRightsDialog(scope.row)">分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 定义分配菜单页面-->
    <el-dialog title="分配权限" :visible.sync="rightsDialogVisible" width="40%" @close="rightsClose">
      <!-- 定义树形空间 show-checkbox定义复选框   node-key="指定节点key" -->
      <el-tree :data="rightsList" :props="defaultProps" show-checkbox node-key="id" default-expand-all
               :default-checked-keys="rightscheckedKeys" ref="rightsTree"></el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="rightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitRights">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  created() {
    this.getRoleList()
  },
  data() {
    return {
      //定义所有角色列表
      roleList: [],
      rightsDialogVisible: false,
      rightsList: [],
      //定义树形控件对象  children:定义父子级关系组件  label: 节点名称
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      rightscheckedKeys: [],
      roleId: ''
    }
  },
  methods: {
    async getRoleList() {
      //发送ajax请求,获取角色列表
      const {
        data: result
      } = await this.$http.get('/role/getRoleList')
      if (result.status !== 200) return this.$message.error("查询角色列表失败")
      this.roleList = result.data
    },
    async deleteRights(role, rights, level) {
      /* 定义消息确认框 */
      let confirmResult = await this.$confirm('是否删除该选项', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)

      if (confirmResult !== 'confirm') return
      let roleId = role.roleId
      let rightsId = rights.id
      let {data: result} = await this.$http.delete('/role/deleteRoleRights',
          {params: {roleId: roleId, rightsId: rightsId, level: level}})

      if (result.status !== 200) return this.$message.error('删除权限失败')
      this.$message.success('删除权限成功')

      //删除成功之后更新列表数据 刷新列表
      role.rightsList = result.data.rightsList
    },
    async showRightsDialog(role) {
      //获取所有权限列表
      const {data: result} = await this.$http.get("/rights/getRightsListAll")
      if (result.status !== 200) return this.$message.error("查询权限列表失败")
      //获取所有的权限列表
      this.rightsList = result.data
      //获取当前用户的三级列表权限
      const {data: result2} = await this.$http.get("/rights/getThreeRightsIds", {params: {roleId: role.roleId}})

      if (result2.status !== 200) return this.$message.error("查询权限列表失败")
      this.rightscheckedKeys = result2.data
      this.rightsDialogVisible = true
      //将roleId传给data属性
      this.roleId = role.roleId;
    },
    rightsClose() {
      this.rightscheckedKeys = []
    },
    async submitRights() {
      //获取树形空间数据
      let checkKeyArray = [
        ...this.$refs.rightsTree.getCheckedKeys(),
        ...this.$refs.rightsTree.getHalfCheckedKeys()
      ]
      //将获取的数据保存到后台服务器中
      let checkKeyStr = checkKeyArray.join(",")
      const {data: result} = await this.$http.post('/role/submitRights', 'roleId=' + this.roleId + '&rightsIds=' + checkKeyStr)
      if (result.status !== 200) return this.$message.error("保存权限失败")
      this.$message.success("保存权限成功")
      //隐藏对话框
      this.rightsDialogVisible = false
      //刷新列表
      this.getRoleList()
    }
  }
}
</script>

<style>
/* 定义tag标签*/
.el-tag {
  margin: 10px;
}

.borderTop {
  border-top: 1px solid #EEEEEE;
}

.borderbottom {
  border-bottom: 1px solid #EEEEEE;
}

/* global.css 定义全局样式 最小宽度 */
/* 定义标签居中对齐*/
.vcenter {
  display: flex;
  align-items: center;
}


</style>
